package br.com.hsp.system.beans;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import br.com.hsp.organizacao.Exame;
import br.com.hsp.system.repository.ExameRepository;
import br.com.hsp.system.repository.InstituicaoRepository;
import br.com.hsp.system.repository.PacienteRepository;
import br.com.hsp.users.MedicoClinica;


@ManagedBean
public class ExameBean {
	
	@ManagedProperty(value="#{userSessionBean}")
	private UserSessionBean session;
	
	private Exame exame = new Exame();

	private List<Exame> exames;
	
	@PostConstruct
	private void preparaPagina() {
		System.out.println(session.getUserSession().getClass().toString());
		if (session.getUserSession().getClass().toString().contains("Medico") || session.getTempId() != null) {
			EntityManager manager = this.getManager();
			PacienteRepository repository = new PacienteRepository(manager);
			InstituicaoRepository iRepository = new InstituicaoRepository(manager);
			exame.setPaciente(repository.procura(session.getTempId()));
			exame.setMedicoLaboratorio((MedicoClinica) session.getUserSession());
			exame.setMedicoClinica((MedicoClinica) session.getUserSession());
			exame.setLaboratorio(iRepository.procura(Long.parseLong("3")));
		}

	}
	
	public void add () {
		System.out.println("entrou add");
		EntityManager manager = this.getManager();
		ExameRepository exameRepository = new ExameRepository(manager);

		if (this.exame.getId() == null) {
			exameRepository.add(this.exame);
		} else {
			exameRepository.atualiza(this.exame);
		}
		
		this.exame = new Exame();
	}
	
	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ExameRepository repository = new ExameRepository(manager);
		this.exame = repository.procura(id);
	}
	
	
	public void remove() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ExameRepository repository = new ExameRepository(manager);
		repository.remove(id);
		this.exame=null;
		this.exames=null;
	}
	
	
	public List<Exame> getExames() {
		if (this.exames == null) {
			EntityManager manager = this.getManager();
			ExameRepository repository = new ExameRepository(manager);
			this.exames = repository.getLista();
		}
		return this.exames;
	}
	
	
	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public Exame getExame() {
		return exame;
	}

	public void setExame(Exame exame) {
		this.exame = exame;
	}

	public UserSessionBean getSession() {
		return session;
	}

	public void setSession(UserSessionBean session) {
		this.session = session;
	}
	
	
}
